package javaExer;


import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 徐州城
 * @version 1.0
 */
public class 冒泡排序and定制 {
    public static void main(String[] args) {
        int[] arr = {1,0,8,-1};
        //两种定制排序方式
        //自定义
        method(arr, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Integer i1 = (Integer) o1;
                Integer i2 = (Integer) o2;
                return i1 - i2;
            }
        });
        System.out.println(Arrays.toString(arr));

        //使用 ArraySort
        Integer[] num = {1,23,4,5,69};
        Arrays.sort(num,new Comparator(){
            @Override
            public int compare(Object o1, Object o2) {
                int i1 = (Integer) o1;
                int i2 = (Integer) o2;
                return i2 - i1;
            }
        });
        System.out.println(Arrays.toString(num));

    }

    public static void method(int[] arr, Comparator c){
        int temp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (c.compare(arr[j],arr[j+1]) > 0){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
}
